/*
 *                    _ooOoo_
 *                   o8888888o
 *                   88" . "88
 *                   (| -_- |)
 *                   O\  =  /O
 *                ____/`- -'\____
 *              .'  \\|     |//  `.
 *             /  \\|||  :  |||//  \
 *            /  _||||| -:- |||||-  \
 *            |   | \\\  -  /// |   |
 *            | \_|  ''\- -/''  |   |
 *            \  .-\__  `-`  ___/-. /
 *          ___`. .'  /- . -\  `. . __
 *       ."" '<  `.___\_<|>_/___.'  >'"".
 *      | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *      \  \ `-.   \_ __\ /__ _/   .-` /  /
 * ======`-.____`-.___\_____/___.-`____.-'======
 *                    `=- -='
 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *           佛祖保佑       永无BUG
 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 */

package com.softeem.starwars.entity;

import com.softeem.starwars.panel.GamePanel;
import com.softeem.starwars.sound.SoundPool;
import com.softeem.starwars.utils.HitCheckUtils;
import com.softeem.starwars.utils.ImageLoader;
import com.softeem.starwars.utils.Settings;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @Author mrchai 2020/4/9 12:48
 */
public class Aestroid extends Sprite implements Runnable{

    private int speed = 6;
    private int blood = 10;
    private Timer timer;

    @Override
    public void init() {
        //随机小行星
        int num = new Random().nextInt(4)+1;
        BufferedImage img = ImageLoader.load("aestroid/aestroid-"+num+".png");
        setImg(img);
        setWidth(img.getWidth());
        setHeight(img.getHeight());
        setX(Settings.WIDTH);
        //随机敌人y轴
        setY(new Random().nextInt(Settings.HEIGHT-getHeight()));

    }

    @Override
    public void run() {
        while(getX() > -getWidth()){
            //修改x轴位置（敌人移动）
            setX(getX() - speed);
            try {
                for (Bullet b:GamePanel.bullets) {

                    //判断是否与子弹产生碰撞
                    if(HitCheckUtils.isHit(getX(),getY(),getWidth(),getHeight(),b.getX(),b.getY(),b.getWidth(),b.getHeight())){
                        //子弹销毁
                        b.destroy();
                        blood--;
                        if(blood <= 0){
                            //得分增加5分
                            GamePanel.score+=5;
                            //行星销毁
                            destroy();
                            GamePanel.score+=10;
                            new SoundPool(SoundPool.BOOM,false).start();
                            return;
                        }
                    }

                }
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        destroy();
    }

    @Override
    public void draw(Graphics g) {
        g.drawImage(getImg(),getX(),getY(),getWidth(),getHeight(),null);
    }

    @Override
    public void destroy() {
        GamePanel.aestroids.remove(this);
        if(timer != null){
            timer.cancel();
        }
    }
}
